<?php
if( !defined( '_VALID_MOS' ) && !defined( '_JEXEC' ) ) die( 'Direct Access to '.basename(__FILE__).' is not allowed.' );

  // -- webMS Modification --
  // Check if osCommerce environment is already loaded
  if (defined( '_OSCOMMERCE_LOADED' )) {
    if (!DB_SHARED) {
      // Reconnect to osCommerce Database
      tep_db_connect() or die('Unable to connect to database server!');
    }
    // Load osCommerce Globals for Boxes
    global $category_links, $languages_id, $catID, $catID_array, $current_category_id;
    global $request_type, $lng, $cart, $currencies, $language, $navigation, $breadcrumb;
  } else {
    // Load osCommerce Environment for the First Time
    if(!function_exists('tep_db_connect')){
    require('components/'.$option.'/includes/application_top.php');
    }
  }

  $currency_symb_query = tep_db_query("select symbol_left, symbol_right, decimal_point, thousands_point from ".DB_PREFIX."currencies where code='".$currency."'");
  $currency_symb = tep_db_fetch_array($currency_symb_query);
  if( mysql_num_rows(mysql_query("SHOW TABLES LIKE '".DB_PREFIX."compbuild_options'"))==1){

//get builder options
$cbcomp_query = tep_db_query("select * from `".DB_PREFIX."compbuild_options`");
 while ($cbcomp = tep_db_fetch_array($cbcomp_query)){
$pc_system_id= $cbcomp['pc_system_id'];
$pc_system_name= $cbcomp['pc_system_name'];
$pc_system_image= $cbcomp['pc_system_image'];
$pc_system_language= $cbcomp['pc_system_language'];
$pc_system_assembly= $cbcomp['pc_system_assembly'];
$pc_template= $cbcomp['pc_template'];
$pc_use_osc_cart= $cbcomp['pc_use_osc_cart'];
$pc_add_tax= $cbcomp['pc_add_tax'];
$pc_use_dependence= $cbcomp['pc_use_dependence'];
$pc_use_software= $cbcomp['pc_use_software'];
$pc_currency= $cbcomp['pc_currency'];
}

//get builder categories
 $pcount=0;
     $bcomp_query = tep_db_query("select * from `".DB_PREFIX."compbuild_categories` ORDER BY pc_category_id");
 while ($bcomp = tep_db_fetch_array($bcomp_query)){
$pcid[$pcount]= $bcomp['pc_category_id'];
$pccat[$pcount]= $bcomp['pc_category_name'];
$pcimg[$pcount]= $bcomp['pc_category_image'];
$osccat[$pcount]= $bcomp['osc_category_id'];
$pcdep[$pcount]= $bcomp['pc_depends_on'];
$msys_id[$pcount]= $bcomp['pc_system_id'];
$presets[$pcount]= $bcomp['pc_preset_ids'];
$pclang[$pcount]= $bcomp['pc_language'];
 $pcount++; }
} else { echo "<table bgcolor=\"red\"><tr><td><font color=\"yellow\"><B>Tables may not exist, or Database Error</B></font></td></tr></table>"; exit(); }

  require(DIR_WS_LANGUAGES . $language . '/compbuild.php');

  $category_query = tep_db_query("select categories_id,categories_name from `".DB_PREFIX."categories_description`  where language_id='".$languages_id."'");
  while ($categories=tep_db_fetch_array($category_query)){
  $category_names[$categories['categories_id']]=$categories['categories_name'];
  }

$output = $pcount;
for ($ib = 0; $ib < $output; $ib++) {
$cat = $osccat[$ib]; //
   if ($_GET['row'] == $ib) $temp = $cat;
}

   $acount=0;

if ($config_dep==1){
    if($_GET['row']==1){
    $d_query = tep_db_query("select categories_id from ".DB_PREFIX."products_to_categories where products_id='".$dcpu."'");
      $depends=tep_db_fetch_array($d_query);
      if ($depends['categories_id']==$dcpuintel) $temp=$dmotherintel;
       else $temp=$dmotheramd;
       $motherboard=$temp;
              }
        if($_GET['row']==2){
             if ((eregi("DDR",$dem) || eregi("ddr",$dmem)) && !(eregi("DDR2",$dem) || eregi("ddr2",$dmem)) && !(eregi("DDR3",$dem) || eregi("ddr3",$dmem)))
             {$temp=$dmemddr;}
             if (eregi("DDR2",$dem) || eregi("ddr2",$dmem))
              {$temp=$dmemddr2;}
             if (eregi("DDR3",$dem) || eregi("ddr3",$dmem))
               {$temp=$dmemddr3;}
            if (eregi("SDRAM",$dem) || eregi("sdram",$dmem))
               {$temp=$dmemsdram;}
            $memory=$temp;
              }
    }


   $a_query = tep_db_query("select categories_id  from ".DB_PREFIX."categories where parent_id='".$temp."'");
     while ($acat=tep_db_fetch_array($a_query)){
           $acategory[$temp][$acount]=$acat['categories_id'];
           $acount++;
     }
$count1=1;
$count2=0;

$textshow[$temp].="print_deselect('"."<font color=\"red\">".TEXT_DESELECT_ITEM."</font>"."','".$_GET['row']."');\n";

    $c_query = tep_db_query("select products_id  from ".DB_PREFIX."products_to_categories where categories_id='".$temp."'");
         while ($count = tep_db_fetch_array($c_query)){
          $a_query = tep_db_query("select products_name  from ".DB_PREFIX."products_description where (products_id='".$count['products_id']."' AND language_id='".$languages_id."')");
             while ($aount = tep_db_fetch_array($a_query)){
             $product ['name'] = addslashes($aount['products_name']);
             $product ['id']= $count['products_id'];

if ($pc_add_tax == 1) { $b_query = tep_db_query("select products_price,products_tax_class_id from ".DB_PREFIX."products where products_id='".$count['products_id']."'");
} else { $b_query = tep_db_query("select products_price from ".DB_PREFIX."products where products_id='".$count['products_id']."'");
     }

             $temps=tep_db_fetch_array($b_query);
             $product ['price'] = '&nbsp;'. $currencies->display_price2($temps['products_price'],tep_get_tax_rate($temps['products_tax_class_id'])).'&nbsp;';
            $ccount++;
          }
         $textshow[$temp].= "print_line('".$count2."','".$product ['name']."','".$product ['price']."','".$product ['id']."','".$_GET['row']."'); \n";
        $count2++;
       }

   for ($f = 0, $z = $acount; $f < $z; $f++) {
        $textshow[$temp].="print_title('".$category_names[$acategory[$temp][$f]]."'); \n";
        $count2++;
         $c_query = tep_db_query("select products_id  from ".DB_PREFIX."products_to_categories where categories_id='".$acategory[$temp][$f]."'");
         while ($count = tep_db_fetch_array($c_query)){
          $a_query = tep_db_query("select products_name  from ".DB_PREFIX."products_description where (products_id='".$count['products_id']."' AND language_id='".$languages_id."')");
             while ($aount = tep_db_fetch_array($a_query)){
             $product ['name'] = addslashes($aount['products_name']);
             $product ['id']= $count['products_id'];

if ($pc_add_tax == 1) { $b_query = tep_db_query("select products_price,products_tax_class_id from ".DB_PREFIX."products where products_id='".$count['products_id']."'");
} else { $b_query = tep_db_query("select products_price from ".DB_PREFIX."products where products_id='".$count['products_id']."'");
     }

             $temps=tep_db_fetch_array($b_query);
             $product ['price'] = '&nbsp;'. $currencies->display_price2($temps['products_price'],tep_get_tax_rate($temps['products_tax_class_id'])).'&nbsp;';
           $ccount++;
            }
        $textshow[$temp].= "print_line('".$count2."','".$product ['name']."','".$product ['price']."','".$product ['id']."','".$_GET['row']."'); \n";
        $count2++;
        }
     } $count1++;
     if ($count2==0) $textshow[$temp].="print_title('".TEXT_NO_ITEMS."'); \n";
     else $textshow[$temp].="print_deselect('"."<font color=\"red\">".TEXT_DESELECT_ITEM."</font>"."','".$_GET['row']."');\n";
?>
<html><head>
<style>TD,TH,BODY{font-family:Arial,Tahoma,Helvetica,sans-serif;font-size:9pt;}TH{background-color:#86A5D2;filter:progid:DXImageTransform.Microsoft.Gradient(GradientType=0,StartColorStr='#0086A5D2',EndColorStr='#C0FFFFFF');font-style:italic;border:1px solid Black;cursor:default;}.select_table{BORDER-COLLAPSE:collapse;border:1px ridge;background-color:#F5F5F5;}tr{cursor:hand;}BODY{overflow: auto;margin: 0 0 0 0;background-color: #F5F5F5;}</style>
<script language="JavaScript">
function print_line(pnum,pdesc,pprice,precid,row){
        var currency_left="<?php echo $currency_symb['symbol_left'];?>";
        var currency_right="<?php echo $currency_symb['symbol_right'];?>";
        var pdesc2 = pdesc.replace(/:inc:/gi, '"')
        var currency_left="<?php echo $currency_symb['symbol_left'];?>";
        var currency_right="<?php echo $currency_symb['symbol_right'];?>";
        re2 = new RegExp ('\'', 'gi') ;
        pdesc = pdesc.replace(re2, ":tag:")
        pprice = pprice.replace(currency_left,"")
        pprice = pprice.replace(currency_right,"")
//        pprice = pprice.replace(",",".")
        //snip
        pprice = pprice.replace("<?php echo $currency_symb['decimal_point'];?>",".")
//        pprice = pprice.replace("<?php echo $currency_symb['thousands_point'];?>",",")

// no calculation possible with thousands seperation
        pprice = pprice.replace("<?php echo $currency_symb['thousands_point'];?>","")
//

        //snip
        document.write ("<tr onclick=\"parent.document.mainform.elements['products_id["+row+"]'].value='"+precid+"';parent.add_product('"+pnum+"','"+pdesc+"','"+pprice+"','"+precid+"','"+row+"');\" onmouseover=\"this.style.backgroundColor='#86A5D2';\" onmouseout=\"this.style.backgroundColor='';\">"
+"<td>"+pdesc2+"</td><td align='right' width=80>&nbsp;"+currency_left+" "+pprice+" "+currency_right+"</td></tr>");
}

function print_title(title){
        document.write("<tr><th align='center' colspan='2'>"+title+"</th></tr>");
}
function print_deselect(title,pindex){
        document.write("<tr onmouseover=\"this.style.backgroundColor='#86A5D2';\" onmouseout=\"this.style.backgroundColor='';\" onclick=\"parent.add_product('','','','','"+pindex+"');\"><td align='center' colspan=2><b>"+title+"</b></td></tr>");
}
</script>
</head>

<body>
<table style="BORDER-COLLAPSE: collapse" borderColor="#86a5d2" border="1" width="100%">
<script language='JavaScript'>
<?php
if ($_GET['row']==$_GET['assemb_id'])
{
   $textshow['assembly'].="print_deselect('"."<font color=\"red\">".TEXT_DESELECT_ITEM."</font>"."','".$_GET['pindex']."');\n";
   $textshow['assembly']="print_title('".ASSEMBLY."'); \n";
   $c_query = tep_db_query("select products_id  from ".DB_PREFIX."products_to_categories where categories_id='0'");
   $count2=0;
   while ($count = tep_db_fetch_array($c_query)){
          $a_query = tep_db_query("select products_name  from ".DB_PREFIX."products_description where (products_id='".$count['products_id']."' AND language_id='".$languages_id."')");
             while ($aount = tep_db_fetch_array($a_query)){
             $assembly_fees['name'] = addslashes($aount['products_name']);
             $assembly_fees['id']= $count['products_id'];
             if ($pc_add_tax == 1) { $b_query = tep_db_query("select products_price,products_tax_class_id from ".DB_PREFIX."products where products_id='".$count['products_id']."'");
             } else { $b_query = tep_db_query("select products_price from ".DB_PREFIX."products where products_id='".$count['products_id']."'");
               }
             $temps=tep_db_fetch_array($b_query);
             $assembly_fees['fee'] = '&nbsp;'. $currencies->display_price2($temps['products_price'],tep_get_tax_rate($temps['products_tax_class_id'])).'&nbsp;';
           $ccount++;
            }
        $textshow['assembly'].= "print_line('".$count2."','".$assembly_fees ['name']."','".$assembly_fees ['fee']."','".$assembly_fees ['id']."','".$_GET['row']."'); \n";
//        $textshow['assembly'].= "print_line('".$count2."','".$product ['name']."','".$product ['price']."','".$product ['id']."','".$_GET['row']."'); \n";
        $count2++;
        }
   $textshow['assembly'].="print_deselect('"."<font color=\"red\">".TEXT_DESELECT_ITEM."</font>"."','".$_GET['row']."');\n";
//print_r($assembly_fees);
   echo $textshow['assembly'];
}
else
{
    $output = $pcount;
    for ($ib = 0; $ib < $output; $ib++) {
    $cat = $osccat[$ib]; //
    if ($_GET['row'] == $ib) echo $textshow[$cat];
    }
}
?>
</script>
</body>
</html>
